// pages/addcar/addcar.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chosety:false,
 
    xphoto: '/images/phone_01.png',
    yphoto: '/images/phone_01.png',
    cphoto: '/images/phone_01.png',
    cphotosdd:'/images/phone_01.png',
    cphotoszz:'/images/phone_01.png',
    cphotosff:'/images/phone_01.png',
    cphotoskk:'/images/phone_01.png',
    yphoto111: '/images/phone_01.png',
    xphoto1: '',
    yphoto1: '',
   
    cphoto1: '',
    cphotosdd1: '',
    cphotoszz1: '',
    cphotosff1: '',
    cphotoskk1: '',
    car_type:1,
    lease_img:'',
    user_type:1,
    car_length_1:[],
    car_type_1:[],
    selCarLen:'',
    selCarType:'',
    // imglist:[
    //   '/images/phone_01.png','/images/phone_01.png'
    // ],
    array: ['车厢1', '车厢3', '车厢4', '车厢5'],
    modelss: [
      { text: '不限' },
      { text: '平板' },
      { text: '高栏车' },
      { text: '底栏车' },
      { text: '高低平板' },
    ],
    items: [
      { name: 'yes', value: '普通货车' },
      { name: 'no', value: '半挂车', checked: 'true' },
    ],
    reg_hqyzm: "获取验证码",
    countdown_time: 60,
    showtime1: true,
    showtime2: false,
  },
  reg_yanzhengma_huoqu: function () {
    console.log("004");
    this.setData({
      showtime1: false,
      showtime2: true,
    });
    countdown(this);
    // wxTimer.start(this); 
  },
  submit:function(e){
    var param = e.detail.value;
    var carInfo = {
      car_no:param.car_no,
      traffic_permit: param.traffic_permit,
      traffic_permit_path: this.data.yphoto1,
      car_type: this.data.car_type,
      business_license: param.business_license,
      traffic_permit_validity: param.traffic_permit_validity,
      vehicle_license_path: this.data.xphoto1,
      head_car_img: this.data.cphoto1,
      car_models: this.data.selCarType,
       
      lease_img: this.data.lease_img,
      car_length: this.data.selCarLen,
      car_length_type: "默认车长类型",
      car_width: param.car_width,
      car_height: param.car_height,
      max_carrier: param.max_carrier
    };
    var userInfo = {
      phone: param.phone,
      name: param.name,
      id_card: param.id_card,
      region: param.region,
      approve_type : 1,
      id_card_front: this.data.cphotosdd1,
      id_card_backz: this.data.cphotoszz1,
      id_card_handle: this.data.cphotosff1,
      driving: this.data.cphotoskk1,
      siji_password: param.siji_password
    };
    var that = this;
    util.commonRequest('addCar', 'post', { carInfo: carInfo, userInfo: userInfo}, function (res) {
      if (res.data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
       
        app.navTo('../CheGuan/CheGuan?status=1&index=1')
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });
    
  },
  initCartLength: function () {
    var that = this;
    util.commonRequest('carLength', 'post', {}, function (res) {
      if (res.data.code == 200) {
        let tempData = res.data.data;
        let tempLen = tempData.length;
        tempData.splice(0, 1);
        that.setData({
          car_length_1: tempData
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });
  },
  initCartType: function () {
    var that = this;
    util.commonRequest('carType', 'post', {}, function (res) {
      if (res.data.code == 200) {
        let tempData = res.data.data;
        let tempLen = tempData.length;
        tempData.splice(0, 1);

        that.setData({
          car_type_1: tempData
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });
  },

  //车长选择
  bindPickerChange: function (e) {
    
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      selCarLen: this.data.car_length_1[e.detail.value]['title']
    
    })
  },
  //车型
  bindPickerChange1:function(e){
    this.setData({
      selCarType: this.data.car_type_1[e.detail.value]['title']
    })
  },
  //查看图片
  previewImage(e){
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imglist // 需要预览的图片http链接列表  
    })
  },
  //行驶证照片
  xphotos: function () {
    var that = this;
    var xphoto = that.data.xphoto;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 3);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          xphoto: res.tempFilePaths
        })
      }
    })
  },


  cphotosdd: function () {
    var that = this;
    var cphotosdd = that.data.cphotosdd;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 4);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          cphotosdd: res.tempFilePaths
        })
      }
    })
  },
  cphotoszz: function () {
    var that = this;
    var cphotoszz = that.data.cphotoszz;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 5);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          cphotoszz: res.tempFilePaths
        })
      }
    })
  },
  cphotosff: function () {
    var that = this;
    var cphotosff = that.data.cphotosff;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 6);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          cphotosff: res.tempFilePaths
        })
      }
    })
  },
  cphotoskk: function () {
    var that = this;
    var cphotoskk = that.data.cphotoskk;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 7);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          cphotoskk: res.tempFilePaths
        })
      }
    })
  },
  
  //运输证
  yphotos: function () {
    var that = this;
    var xphoto = that.data.yphoto;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 1);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          yphoto: res.tempFilePaths
        })
       
      }
    })
  },
  yphotos22: function () {
    var that = this;
    var xphoto = that.data.yphoto;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 21);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          yphoto111: res.tempFilePaths
        })

      }
    })
  },
  
  //车头
  cphotos: function () {
    var that = this;
    var cphoto = that.data.cphoto;
    wx.chooseImage({
      count: 1, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
      success: function (res) {
        that.uploadImage(res, 2);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
        that.setData({
          cphoto: res.tempFilePaths
        })
      }
    })
  },
  uploadImage: function (res, type) {
    let file = res.tempFilePaths[0];
    var that = this;
    wx.uploadFile({
      url: app.data.base_url + 'uploadImg',
      filePath: file,
      name: 'image',
      success: function (res) {
        var res = JSON.parse(res.data);

        if (res.code == 200) {
          if (type == 1) {
            that.setData({ yphoto: res.data, yphoto1: res.data })
          } else if (type == 2) {

            that.setData({ cphoto: res.data, cphoto1: res.data })
          } else if (type == 3) {
            that.setData({ xphoto: res.data, xphoto1: res.data })
          } else if (type == 4) {
            that.setData({ cphotosdd: res.data, cphotosdd1: res.data })
          } else if (type == 5) {
            that.setData({ cphotoszz: res.data, cphotoszz1: res.data })
          } else if (type == 6) {
            that.setData({ cphotosff: res.data, cphotosff1: res.data })
          } else if (type == 7) {
            that.setData({ cphotoskk: res.data, cphotoskk1: res.data })
          } else if (type == 21) {
            that.setData({ yphoto111: res.data, lease_img: res.data })
          }

        } else {
          wx.showToast({
            title: res.msg
          })
        }
      }
    })
  },
  // 是否续费
  radioChange: function (e) {
    var v = e.detail.value;
    if (v){
      this.setData({
        car_type:0
      })
    } else{
      this.setData({
        car_type: 1
      })
    }
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initCartLength();
    this.initCartType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var user_type = wx.getStorageSync('userInfo').user_type;
    this.setData({
      user_type:user_type
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function countdown(that) {
  var second = that.data.countdown_time
  if (second == 0) {
    // console.log("001");
    that.setData({
      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });
    // console.log("002");
    return;
  }
  // console.log("003");
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  }
    , 1000)
}